package com.hxx.config;

import com.hxx.ai.Aissistant;
import com.hxx.ai.HxxTools;
import com.hxx.ai.InvoceHandler;
import dev.langchain4j.agent.tool.ToolExecutionRequest;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.ollama.OllamaChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.tool.ToolExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static dev.langchain4j.agent.tool.JsonSchemaProperty.description;
import static dev.langchain4j.agent.tool.JsonSchemaProperty.type;

@Configuration
public class MyConfig {

    @Bean
    public ChatLanguageModel chatLanguageModel() {
        return OllamaChatModel
                .builder()
                .modelName("qwen2.5:3b")
                .baseUrl("http://localhost:11434")
                .build();
    }

    @Bean
    public Aissistant aissistant(ChatLanguageModel chatLanguageModel) {
        //工具的描述
        ToolSpecification specification = ToolSpecification.builder()
                .name("invoceHandler")
                .description("根据发票号码查询发票信息")
                .addParameter("companyName", type("string"), description("公司名称"))
                .addParameter("dutyNumber", type("string"), description("公司的税号"))
                .addParameter("amount", type("string"), description("金额保留两位有效数字"))
                .build();
        //工具的业务逻辑
        ToolExecutor toolExecutor =(toolExecutionRequest, memoryId)-> {
            return toolExecutionRequest.arguments();
        };

        return AiServices.builder(Aissistant.class)
                .chatLanguageModel(chatLanguageModel)
                .chatMemoryProvider(cmp-> MessageWindowChatMemory.withMaxMessages(100))
                .tools(new InvoceHandler())
                .tools(specification, toolExecutor)
                .build();
    }
}



























